package pkg.chemdrm;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.HorizontalAlign;

import android.content.Context;

public class MenuScene extends ChemScene implements ITimerCallback {
	final static float FPS = 60; // try to force this FPS; note it is possible to fall below this frame rate.
	final static float FRAME_PERIOD = 1f/FPS;
	
	private TimerHandler mTimerHandler;
	
	/* Resources */
	
	/*
	private Sprite mTitleSprite;
	private Sprite mButtonNewGame;
	private Sprite mButtonHelp;
	*/
	private final Text mChemicalDreamText;
	private final Text mTBD;
	private final Text mHelpText;
	private final Text mPlayText;
	
	private float mElapsedTime;
	
	public MenuScene(final Context pContext, final SceneHandler pSceneHandler, final ResourceHandler pResourceHandler, final BoundCamera pCamera)
	{
		super(pContext, pSceneHandler, pResourceHandler, pCamera);

		// this tells the engine to call the method onTimePassed every FRAME_PERIOD seconds (auto repeat) 
		mTimerHandler = new TimerHandler(FRAME_PERIOD, true, this);
		registerUpdateHandler(mTimerHandler);
		
		
		buildScene();
		
		// Jerome: I just created a single default font for now, let me know if you want more since I can easily create
		// an interface that supports more. Notice that we can override the onAreaTouched of a Text class to allow us to
		// use it for moving between scenes. Though there is only a single MenuScene, and you can do whatever you want with it.
		// though you could probably get a decent menu system going by just hide()/show() the appropriate menu items. 
		
		// Turned Title into non-button logo
		mChemicalDreamText = new Text(100, 50, mResourceHandler.getFont(), "Chemical Dream", HorizontalAlign.LEFT);
		
		// Added team name
		mTBD = new Text(60, 100, mResourceHandler.getFont(), "Team Be Determined", HorizontalAlign.LEFT);
		
		// Press play to start the game
		mPlayText = new Text(260, 250, mResourceHandler.getFont(), "Play", HorizontalAlign.LEFT)
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
			{
				// when we touch the title, go to the current working game.
				mSceneHandler.setScene("Game");
				return true;
			}
		};
		
		// Press "Help" to go to "help" scene. Need to add text
		mHelpText = new Text(100, 250, mResourceHandler.getFont(), "Help", HorizontalAlign.LEFT)
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
			{
				// when we touch the title, go to the Help section.
				mSceneHandler.setScene("Help");
				return true;
			}
		};
		
		attachChild(mChemicalDreamText);
		attachChild(mHelpText);
		attachChild(mPlayText);
		attachChild(mTBD);
		
		registerTouchArea(mHelpText);
		registerTouchArea(mPlayText);

	}
	
	public void onTimePassed(final TimerHandler pTimerHandler) {
		// this gets called every frame (when this scene is active)
		
		float dt = pTimerHandler.getTimerSecondsElapsed();

		mElapsedTime += dt;
		
		//mTitleSprite.setPosition(0+5f*(float)Math.cos(0.5f*mElapsedTime), 10+1f*(float)Math.sin(0.5f*mElapsedTime));
		
	}
	
	public Camera getCamera()
	{
		return mCamera;
	}
	
	public void restoreCamera()
	{
		mCamera.setCenter(0, 0);
		if(mCamera.hasHUD())
			mCamera.getHUD().setVisible(false);
		
	}
	
	private void buildScene()
	{		
		/* Initialize Sprites */
		//// presentation
		// title				

		// Jerome: I just left this stuff here for note purposes. You can use sprites for menu items, or just use text, just remember t
		// override the onAreaTouched method, and then call this.registerTouchArea and pass in the object
		/*
		mTitleSprite = new Sprite(0, 10, mResourceHandler.getRegion("MainMenu_TitleRegion"))
		{ 
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
			{
				// when we touch the title, go to the current working game.
				mSceneHandler.setScene("Game");
				return true;
			}
		};
			*/
		//mTitleSprite.setWidth(300);
		//mTitleSprite.setHeight(128);

		/* Attach Sprites and Register them for touches.*/
		//attachChild(mTitleSprite);
		//registerTouchArea(mTitleSprite);
		/* Misc */
		
		setBackground(new ColorBackground(1f, 1f, 1f));
		
		
	}

}
